package gui.inventory;

import gui.product.ProductData;
import hit.core.ItemNotification;
import hit.core.Product;
import hit.core.ProductContainer;

import java.util.Observable;
import java.util.Observer;

public class ItemObserver implements Observer {
	private IInventoryView view;
	private InventoryController controller;
	
	/**
	 * @param view the view the observer belongs to
	 * @param controller the controller the observer uses
	 * */
	public ItemObserver(IInventoryView view, InventoryController controller) {
		this.view = view;
		this.controller = controller;
	}
	
	@Override
	public void update(Observable o, Object response) {
		ItemNotification notification = (ItemNotification) response;
		
		ProductContainerData selectedContainer = view.getSelectedProductContainer();
		ProductContainer container = (ProductContainer)selectedContainer.getTag();
		
		ProductData selectedProduct = view.getSelectedProduct();
		
		switch(notification.getType()) {
		case REMOVE_ITEM:
			controller.DrawProducts(container, true);

			if (selectedProduct != null) {
				Product product = (Product)selectedProduct.getTag();
				selectedProduct.setCount(
						new Integer(notification.getModifiedItems().size()).toString());
				controller.DrawItems(container, product);
			}
	
			break;
		case EDIT_ITEM:
			if (selectedProduct != null) {
				Product product = (Product) selectedProduct.getTag();
				controller.DrawItems(container, product);
			}
			break;
			
		default:
			if (selectedProduct != null) {
				Product product = (Product)selectedProduct.getTag();
				
				controller.DrawItems(container, product);
			}
			
			controller.DrawProducts(container, true);
			
			break;
			
		}
	}
}
